<?php
namespace Duobao\Controller;

use Duobao\Model\CartModel as Cart;

class CartController extends ControllerBase{
    /**
     * 默认
     */
    public function index()
    {
        echo "<!--hi , i'm from " . MODULE_NAME . "~" . APP_STATUS, "-->";
    }

    /**
     * 判断一种商品能否加入购物车
     */
    public function judgmentJoinCart(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = I('get.id');

            if(!empty($id)){
                $model = new Cart();
                $result = $model->judgmentJoinCart($id);
                if($result){
                   $ret['data']['status'] = true;
                }else{
                  $ret['data']['status'] = false;
                }

            }else{
                $ret['status'] = 403;
                $ret['msg'] = 'empty of the goods id';
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 前端提交一系列的产品id，返回商品的数据，商品的img,商品名称，总需人数，剩余人数
     * 接受id组成的字符串接收前台的数据
     */
    public function listCart(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $idstr = I('post.idstr');

            if(!empty($idstr)){
                $model = new Cart();
                $result = $model->listCart(explode(',',$idstr));

                if($result){
                    $ret['data']['item'] = $result;
                }else{
                    $ret['status'] = 200;
                    $ret['msg'] = '更具传递的id数据库获取不到相应的值';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = 'empty of cart id list';
            }
        }catch(\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /* end of Cart controller */
}